package com.xzy.rabbit.common.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

/**
 * RabbitMQ工具类
 *
 * @author xzy
 * @date 2021年9月23日
 */
@Slf4j
public class RabbitUtil {
    public static final ConnectionFactory CONNECTION_FACTORY = new ConnectionFactory();

    private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(3);

    static {
        CONNECTION_FACTORY.setHost("127.0.0.1");
        CONNECTION_FACTORY.setPort(5672);
        CONNECTION_FACTORY.setUsername("admin");
        CONNECTION_FACTORY.setPassword("admin");
        CONNECTION_FACTORY.setConnectionTimeout(300000);

        // 使用自定义的线程池
        CONNECTION_FACTORY.setSharedExecutor(EXECUTOR_SERVICE);
    }

    public static Connection getConnection() throws IOException, TimeoutException {
        return CONNECTION_FACTORY.newConnection();
    }
}
